<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/20
 * Time: 10:19
 */
namespace app\member\controller;
use think\Db;
use think\Session;
use think\Config;
use think\Request;
use think\Loader;
use app\common\controller\simple_html_dom;
use app\member\model\Balance as BalanceModel;
use app\member\model\Changedoc as ChangedocModel;
class Order extends Common
{
	public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
		parent::checktype();
    }
    /**
     * 订单列表
     */
    public function index()
    {
        session('MenuCookie','2');
        $UrlParam=input();
        $where=array();
        if(!empty($UrlParam['order'])) $where['A.order_number'] = ['like','%'.$UrlParam['order'].'%'];
        if(!empty($UrlParam['title'])) $where['W.article_title'] = ['like','%'.$UrlParam['title'].'%'];
		if(!empty($UrlParam['article_customer'])) $where['W.article_customer'] = ['like','%'.$UrlParam['article_customer'].'%'];		
        if(isset($UrlParam['status']) && $UrlParam['status']>='0'){			
			$where['A.order_state'] = $UrlParam['status'];
		}else{
			$UrlParam['status']=-1;
		}
        $StartDate = input('startdate', '');
        $EndDate = input('enddate', '');
        if(!empty($StartDate) || !empty($EndDate)){
            if(empty($StartDate)) $StartDate=date('Y-m-d');
            if(empty($EndDate)) $EndDate=date('Y-m-d');
        }
        if(!empty($StartDate) && !empty($EndDate)){
            $StartDate=strtotime($StartDate);$EndDate=strtotime($EndDate);
            $MinDate=min($StartDate,$EndDate);$MaxDate=max($StartDate,$EndDate);
            $MaxDate+=86399;
            $UrlParam['startdate']=date('Y-m-d',$MinDate);
            $UrlParam['enddate']=date('Y-m-d',$MaxDate);
            $UrlParam['MinDate']=$MinDate;$UrlParam['MaxDate']=$MaxDate;
        }
        if(!empty($UrlParam['MinDate']) && !empty($UrlParam['MaxDate'])){
            $where['A.order_time']=array('between',[$UrlParam['MinDate'],$UrlParam['MaxDate']]);
        }
        $where['W.member_id']=$this->MemberID;
        $where['A.RegDomain']=$this->AgencySiteInfo['Domain'];
		$where['A.media_type']=1;
        $List =  Db::name('agencyorder')
            ->alias('A')
            ->field('A.*,W.article_title,W.article_customer,M.MediaTitle,M.UserType,W1.TypeName as mtmc')
            ->join('article W', 'W.article_id=A.article_id', 'left')
            ->join('media M', 'M.MediaID=A.media_id', 'left')
			->join('wholetype W1', 'W1.TypeID=M.MediaWebNameID', 'left')//频道类型
            ->where($where)
            ->order('A.order_time desc')
            ->paginate(20,true,['query'  => $UrlParam]);
		//echo Db::name('agencyorder')->getLastsql();
		//die;
        $_SESSION['UrlParamOrder']=$UrlParam;
        $this->assign('UrlParam',$UrlParam);
        $this->assign('list',$List);
        return view();
    }
    /**
     * 取消稿件
     
    public function cancel(){
        $ID=input('ID','','abs');
        $order_state = 4;
        $fabu_site = '';
        $fabu_content = '';
        $Contrller=controller('common/Common');
        $Contrller->Refund($ID,$order_state,$fabu_site,$fabu_content);
        $this->success('操作成功！');
    }*/
	//申请退稿

    public function reback()
    {
		$orderid=input('ID','','abs');
		$order=db('agencyorder')->where('agencyorder_id','eq',$orderid)->find();
		$datas=[];
		$datas['dkid']=$order['order_number'];
		$datas['apiKey']='6f571fc6a99fcf83e3bbdd8dd04d2127';
		$datas['type']='reback';
		$rs=json_decode(curl_w('http://www.meijiedaka.com/v3/orderList',$datas));
		if($rs->code==200){
			db('agencyorder')->where('agencyorder_id','eq',$order['agencyorder_id'])->where('media_type','eq',1)->update(['order_state'=>9]);
			 $this->success('操作成功！');
		}else{
			 $this->error($rs->msg);
		}
    }
	//取消申请退稿
	public function unback()
    {
		$orderid=input('ID','','abs');
		$order=db('agencyorder')->where('agencyorder_id','eq',$orderid)->find();
		$datas=[];
		$datas['dkid']=$order['order_number'];
		$datas['apiKey']='6f571fc6a99fcf83e3bbdd8dd04d2127';
		$datas['type']='unback';
		$rs=json_decode(curl_w('http://www.meijiedaka.com/v3/orderList',$datas));		
		if($rs->code==200){
			db('agencyorder')->where('agencyorder_id','eq',$order['agencyorder_id'])->where('media_type','eq',1)->update(['order_state'=>1]);
			 $this->success('操作成功！');
		}else{
			 $this->error($rs->msg);
		}
    }
    //导出我的媒体订单
    public function exportorderexcel()
    {
        $name='我的订单';
        $Info=$this->GetOrderListFull($_SESSION['UrlParamOrder']);
        if(!empty($Info))
        {
            //获取标题
            if(sizeof($Info)>1000) $this->alert('导出数据不能超过1000条，请筛选后再导出','back');
            $TitleInfo=GetExportTitle('memberorder');
            excelExport($name,$TitleInfo,$Info);
        }else{
            $this->alert('导出的数据为空','back');
            exit;
        }
    }
    public function GetOrderListFull($UrlParamOrder)
    {
        $where=array();
        if(!empty($UrlParamOrder['order'])) $where['A.order_number'] = ['like','%'.$UrlParamOrder['order'].'%'];
        if(!empty($UrlParamOrder['title'])) $where['W.article_title'] = ['like','%'.$UrlParamOrder['title'].'%'];
        $StartDate =!empty($UrlParamOrder['startdate'])? $UrlParamOrder['startdate']:'';
        $EndDate =!empty($UrlParamOrder['enddate'])? $UrlParamOrder['enddate']:'';
        if(!empty($StartDate) || !empty($EndDate)){
            if(empty($StartDate)) $StartDate=date('Y-m-d');
            if(empty($EndDate)) $EndDate=date('Y-m-d');
        }
        if(!empty($StartDate) && !empty($EndDate)){
            $StartDate=strtotime($StartDate);$EndDate=strtotime($EndDate);
            $MinDate=min($StartDate,$EndDate);$MaxDate=max($StartDate,$EndDate);
            $MaxDate+=86399;
            $UrlParamOrder['startdate']=date('Y-m-d',$MinDate);
            $UrlParamOrder['enddate']=date('Y-m-d',$MaxDate);
            $UrlParamOrder['MinDate']=$MinDate;$UrlParamOrder['MaxDate']=$MaxDate;
        }
        if(!empty($UrlParamOrder['MinDate']) && !empty($UrlParamOrder['MaxDate'])){
            $where['A.order_time']=array('between',[$UrlParamOrder['MinDate'],$UrlParamOrder['MaxDate']]);
        }
        $where['W.member_id']=$this->MemberID;
        $where['A.RegDomain']=$this->AgencySiteInfo['Domain'];
		$where['A.media_type']=1;
        $List =  Db::name('agencyorder')
            ->alias('A')
            ->field('A.*,W.article_title,M.MediaTitle')
            ->join('article W', 'W.article_id=A.article_id', 'left')
            ->join('media M', 'M.MediaID=A.media_id', 'left')
            ->where($where)
            ->order('A.article_id desc')
            ->select();
        if(!empty($List)){
            foreach($List as $key=>$val) {
                if ($val['order_state']==0)
                {
                    $List[$key]['order_statename']='待审核';
                }elseif($val['order_state']==1)
                {
                    $List[$key]['order_statename']='发布中';
                }elseif($val['order_state']==2)
                {
                    $List[$key]['order_statename']='已发布';
                }elseif($val['order_state']==3)
                {
                    $List[$key]['order_statename']='已退稿';
                }elseif($val['order_state']==4)
                {
                    $List[$key]['order_statename']='已取消';
                }
                if(!empty($val['order_time']))
                {
                    $List[$key]['order_stateaffirmtime']=date("Y-m-d H:i:s",$val['order_time']);
                }
                if(!empty($val['release_time']))
                {
                    $List[$key]['order_statereleasetime']=date("Y-m-d H:i:s",$val['release_time']);
                }else $List[$key]['order_statereleasetime']='';
            }
        }
        return $List;
    }
	/**
     * 我的媒体订单
     */
    public function myorder()
    {
		parent::checkmedia_man();
        session('MenuCookie','2');
        $UrlParam=input();
        $where=array();
        if(!empty($UrlParam['order'])) $where['A.order_number'] = ['like','%'.$UrlParam['order'].'%'];
        if(!empty($UrlParam['title'])) $where['W.article_title'] = ['like','%'.$UrlParam['title'].'%'];
		if(!empty($UrlParam['article_customer'])) $where['W.article_customer'] = ['like','%'.$UrlParam['article_customer'].'%'];
		
        if(isset($UrlParam['status']) && $UrlParam['status']>='0'){
			
			$where['A.order_state'] = $UrlParam['status'];
		}else{
			$UrlParam['status']=-1;
		}
        $StartDate = input('startdate', '');
        $EndDate = input('enddate', '');
        if(!empty($StartDate) || !empty($EndDate)){
            if(empty($StartDate)) $StartDate=date('Y-m-d');
            if(empty($EndDate)) $EndDate=date('Y-m-d');
        }
        if(!empty($StartDate) && !empty($EndDate)){
            $StartDate=strtotime($StartDate);$EndDate=strtotime($EndDate);
            $MinDate=min($StartDate,$EndDate);$MaxDate=max($StartDate,$EndDate);
            $MaxDate+=86399;
            $UrlParam['startdate']=date('Y-m-d',$MinDate);
            $UrlParam['enddate']=date('Y-m-d',$MaxDate);
            $UrlParam['MinDate']=$MinDate;$UrlParam['MaxDate']=$MaxDate;
        }
        if(!empty($UrlParam['MinDate']) && !empty($UrlParam['MaxDate'])){
            $where['A.order_time']=array('between',[$UrlParam['MinDate'],$UrlParam['MaxDate']]);
        }
        //$where['A.RegDomain']=$this->AgencySiteInfo['Domain'];
		$where['A.media_type']=1;
		$where['M.MediaMemberID']=$this->MemberID;
        $List =  Db::name('agencyorder')
            ->alias('A')
            ->field('A.*,W.article_title,W.article_customer,W.article_remarks,M.MediaTitle,M.MediaMemberID,W1.TypeName as mtmc')
            ->join('article W', 'W.article_id=A.article_id', 'left')
            ->join('media M', 'M.MediaID=A.media_id', 'left')
			->join('wholetype W1', 'W1.TypeID=M.MediaWebNameID', 'left')//频道类型
            ->where($where)
            ->order('A.article_id desc')
            ->paginate(20);
		//echo  Db::name('agencyorder')->getlastsql();
			$_SESSION['UrlParamOrder']=$UrlParam;
			$this->assign('UrlParam',$UrlParam);
			$this->assign('list',$List);
			return view();
    }
	/**
     * 收稿
     */
    public function collect(){
		parent::checkmedia_man();
        $orderid=input('orderid');
        $data['order_state']= 1;
		$result=Db::name('agencyorder')->where('agencyorder_id',$orderid)->update($data);
		if($result){
			return json(array('code'=>'200','msg'=>'收稿成功！'));
		}else{
			return json(array('code'=>'201','msg'=>'收稿失败！'));
		}
    }

	/**
     * 发布
    */
    public function publish(){
		parent::checkmedia_man();
        $orderid=input('orderid');
        $data1['order_state']= 2;
		$data1['fabu_site']= input('url');
		$data1['release_time']= time();
		$preg = "/^http(s)?:\\/\\/.+/";
		if(!preg_match($preg,$data1['fabu_site']))
		{
			return json(array('code'=>'201','msg'=>'网址必须带http:// 或 https://'));
		}
		
		Db::startTrans();
		try {
			$info=Db::name('agencyorder')->where('agencyorder_id',$orderid)->find();
			if($info){
				$result=Db::name('agencyorder')->where('agencyorder_id',$orderid)->where('member_id',$this->MemberID)->update($data1);
				$returndata=Db::name('Member')->where('MemberID',$info['member_id'])->setInc('MemberProfit',$info['media_money']);
				$xj = Db::name('Member')->where('MemberID',$info['member_id'])->value('MemberBalanceCount');
				$data['BalanceMemberID'] = $info['member_id'];
				$data['BalanceNumber'] = $info['order_number'];
				$data['BalanceType'] = 1;
				$data['BalanceCount'] = $info['media_money'];
				$data['BalanceCurrentCount'] = $xj;
				$data['BalanceTime'] = time();
				$data['BalanceGroup'] = '5';
				$data['BalanceState'] = '1';
				$data['BalanceTitle'] = '媒体收稿';
				$BalanceModel=new BalanceModel();
				$resultdd=$BalanceModel->insertGetId($data);
				if($result && $resultdd){
					Db::commit();
					return json(array('code'=>'200','msg'=>'发布成功！'));
				}else{
					throw new \Exception(1111);
				}
			}
		}catch (\Exception $e) {
			   // 回滚事务
			    Db::rollback();
				return json(array('code'=>'201','msg'=>'发布失败！'));
			}
    }

	/**
     * 拒稿
    */
    public function refuse(){
		parent::checkmedia_man();
		$ID=input('orderid');
        $order_state= 3;
		$fabu_site='';
		$fabu_content= input('reason');
		$Contrller=controller('common/Common');
		$result=$Contrller->Refund($ID,$order_state,$fabu_site,$fabu_content);
		if($result){
			return json(array('code'=>'200','msg'=>'拒稿成功！'));
		}else{
			return json(array('code'=>'201','msg'=>'拒稿失败！'));
		}
    }


	//文章修改
	public function edit(){
			$ID = input('id');
			if($ID){
				$ArticleInfo=db('article')->where('article_id',$ID)->find();
 				$title = $ArticleInfo['article_title'];
				$beizhu = $ArticleInfo['article_remarks'];
				$editorValue = $ArticleInfo['article_content'];
			}else{
				$this->error('文章不存在');
			}
 			$this->assign('editorValue',$editorValue);
			$this->assign('beizhu',$beizhu);
			$this->assign('title',$title);
			$this->assign('id',$ID);
  			return $this->fetch();
		}

		//保存修改
	public function editsave(){
			$data = input();
			$res='';
			if($data && $data['article_id']>0){
				$res=Db::name('article')->where('article_id',$data['article_id'])->update($data);
 			}
  			if($res){
				$this->error('修改成功');
			}else{
				$this->error('修改失败');
			}
		}

	public function change_edit(){
		$ID = input('ID');
			$info=Db::name('agencyorder')->where('agencyorder_id',$ID)->find();
			if($info){
				$this->assign('id',$ID);
				return $this->fetch();
			}else{
				$this->error('修改失败');
			}
    }

	/**
     * 改稿保存
    */
    public function change_editsave(){
		$UrlParam = input();
		
		if(!$UrlParam['price'] || !$UrlParam['content']){
				return json(array('code'=>'201','msg'=>'请填写金额和改稿说明'));
		}
		$info=Db::name('agencyorder')->where('agencyorder_id',$UrlParam['id'])->find();
		if($info){
			$articleinfo=Db::name('article')->where('article_id',$info['article_id'])->find();
			$data1=array(
				'order_state'=>10
			);
			$result=Db::name('agencyorder')->where('agencyorder_id',$UrlParam['id'])->where('member_id',$this->MemberID)->update($data1);

			

			if($this->MemberInfo['MemberBalanceCount'] < $UrlParam['price']) return json(array('code'=>'201','msg'=>'余额不足，请充值！'));

			$returndata=Db::name('Member')->where('MemberID',$articleinfo['member_id'])->setDec('MemberBalanceCount',$UrlParam['price']);

			$xj = Db::name('Member')->where('MemberID',$articleinfo['member_id'])->value('MemberBalanceCount');
			$data['BalanceMemberID'] = $articleinfo['member_id'];
			$data['BalanceNumber'] = $info['order_number'];
			$data['BalanceType'] = 2;
			$data['BalanceCount'] = $UrlParam['price'];
			$data['BalanceCurrentCount'] = $xj;
			$data['BalanceTime'] = time();
			$data['BalanceGroup'] = '4';
			$data['BalanceState'] = '1';
			$data['BalanceTitle'] = '媒体改稿';
			$BalanceModel=new BalanceModel();
			$resultdd=$BalanceModel->insertGetId($data);

			$data2['memberid'] = $articleinfo['member_id'];
			$data2['ordernum'] = $info['order_number'];
			$data2['articletitle'] = $articleinfo['article_title'];
			$data2['articleid'] = $articleinfo['article_id'];			
			$data2['price'] = $UrlParam['price'];
			$data2['changedc'] = $UrlParam['content'];
			$data2['orderid'] = $UrlParam['id'];
			$data2['mediaid'] = $info['media_id'];
			$data2['addtime'] = time();
			$ChangedocModel=new ChangedocModel();
			$resultdc=$ChangedocModel->insertGetId($data2);

			if($resultdd && $resultdc){
				return json(array('code'=>'200','msg'=>'申请成功！'));
			}else{
				return json(array('code'=>'201','msg'=>'申请失败！'));
			}
		}
    }
}